package com.sh.data.engine.application.service.management;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.HiveSiteXmlUploadDTO;
import com.sh.data.engine.application.model.dto.workspace.BaseStorageDTO;
import com.sh.data.engine.application.model.dto.workspace.StorageDTO;
import com.sh.data.engine.application.model.mapper.integration.FileUploadResultMapper;
import com.sh.data.engine.application.model.mapper.workspace.StorageConnectionParamMapper;
import com.sh.data.engine.application.model.mapper.workspace.StorageDTOMapper;
import com.sh.data.engine.application.model.mapper.workspace.StoragePageMapper;
import com.sh.data.engine.application.model.mapper.workspace.StorageSaveParamMapper;
import com.sh.data.engine.application.model.request.management.SchemaQueryRequest;
import com.sh.data.engine.application.model.request.management.StorageConnectionRequest;
import com.sh.data.engine.application.model.request.management.StorageSaveRequest;
import com.sh.data.engine.common.exception.BusinessException;
import com.sh.data.engine.domain.base.model.FileUploadResultDomain;
import com.sh.data.engine.domain.base.service.FileService;
import com.sh.data.engine.domain.workspace.manager.model.domain.StorageDetailDomain;
import com.sh.data.engine.domain.workspace.manager.model.domain.StorageListDomain;
import com.sh.data.engine.domain.workspace.manager.model.param.StorageConnectionParam;
import com.sh.data.engine.domain.workspace.manager.model.param.StorageSaveParam;
import com.sh.data.engine.domain.workspace.manager.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/24 11:28
 */
@Slf4j
@Service
public class StorageApplicationService {

    @Autowired
    private StorageService storageService;

    @Autowired
    private FileService fileService;

    @Autowired
    private StorageSaveParamMapper storageSaveParamMapper;

    @Autowired
    private StorageConnectionParamMapper storageConnectionParamMapper;

    @Autowired
    private StorageDTOMapper storageDTOMapper;

    @Autowired
    private FileUploadResultMapper fileUploadResultMapper;

    @Autowired
    private StoragePageMapper storagePageMapper;

    @Value("${data.engine.hiveUploadAddress:hive-site/}")
    private String hiveUploadAddress;

    public Boolean saveStorage(StorageSaveRequest storageRequest) {
        StorageSaveParam param = storageSaveParamMapper.map(storageRequest);
        storageService.saveStorage(param);
        return true;
    }

    public Boolean deleteStorage(Long storageId) {
        storageService.deleteStorage(storageId);
        return true;
    }

    public Boolean testStorage(StorageConnectionRequest storageConnectionRequest) {
        StorageConnectionParam param = storageConnectionParamMapper.map(storageConnectionRequest);
        return storageService.testStorage(param);
    }

    public StorageDTO getDetailStorage(Long storageId) {
        StorageDetailDomain storageDetailDomain = storageService.getDetailStorage(storageId, true);
        return storageDTOMapper.domainToDto(storageDetailDomain);
    }

    public PagerResponse<BaseStorageDTO> listStorage(
        Integer pageNum, Integer pageSize, Long lastStorageId, String dsType, String search) {
        var storagePageList = storageService.listStorage(pageNum, pageSize, lastStorageId, dsType, search);
        return storagePageMapper.toPageResponse(storagePageList);
    }

    public List<String> getSchemas(SchemaQueryRequest request) {
        return storageService.getSchemas(
            request.getStorageLink(),
            request.getUsername(),
            request.getPassword(),
            request.getDsType());
    }

    public HiveSiteXmlUploadDTO uploadHiveSiteXml(MultipartFile multipartFile) {
        String hiveSiteAddress = this.hiveUploadAddress + "/" + UUID.randomUUID();
        try {
            FileUploadResultDomain upload = fileService.uploadHiveSite(multipartFile, hiveSiteAddress, false);
            log.info("上传Hive site xml成功");
            return fileUploadResultMapper.domainToDTO(upload);
        } catch (Exception e) {
            throw new BusinessException("上传Hive site xml失败");
        }
    }

    public List<BaseStorageDTO> getAllStorageList() {
        List<StorageListDomain> storageListDomains = storageService.getAllStorageList();
        return storageDTOMapper.map(storageListDomains);
    }
}
